<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>变量交换</title>
    <script>
      let n1 = 5
      let n2 = 10

      //交换两个变量
      //1.临时变量法
      /* let temp = n1 //临时变量temp
      n1 = n2
      n2 = temp */

      //2.加减法
      /*  n1 += n2
       n2 = n1 - n2
       n1 -= n2 */

      //3.异或
      /*  n1 ^= n2
       n2 ^= n1
       n1 ^= n2 */

      //4.对象法
      /* n1 = { n1: n2, n2: n1 }
      n2 = n1.n2
      n1 = n1.n1
     */

      //5.数组法
      /* n1 = [n2, n2 = n1][0]
       */

      //6.ES678解构
      ;[n1, n2] = [n2, n1]
      console.log(`n1 = ${n1}, n2 = ${n2}`)
    </script>
  </head>

  <body></body>
</html>
